﻿package  
{
	import Base.Net.NetSocket;
	
	import Common.GameDefine;
	import Common.GameNetManager;
	import Common.RenderManager;
	
	import Events.LogicEvent;
	import Events.LogicEventManager;
	import Events.ModuleEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	import map.MapGrid;
	import map.MapPos;
	import map.MapScene;
	import map.PathFinder;
	
	import net.NpcNetHandler;
	
	import ui.NpcTalkUIHandler;

	/**
	 * ...
	 * @author fox
	 */
	public class GameScene
	{
		public var mainPlayer:GamePlayer;
		
		
		private var netSocket:NetSocket = null;
		
		private var playerClick:Boolean = false;
		
		private var pos:MapPos = null;
		
		private var lastTime:Number = 0.0;
		private var nowTime:Number = 0.0;
		public var delay:Number = 0.0;
		
		public function GameScene() 
		{
			
		}
		
		public static var instance:GameScene = null;
		public static function Instance():GameScene
		{
			if ( instance == null )
			{
				instance = new GameScene();
			}
			
			return instance;
		}
		
		public function init( ):void
		{
			mainPlayer = GameManager.instance.MainPlayer;
			netSocket = GameNetManager.instance.MainNetSocket;
			
			pos = new MapPos();
			pos.init();
			
			//LogicEventManager.instance.addEventListener( LogicEvent.MOVE , onMove );
			LogicEventManager.instance.addEventListener( LogicEvent.PLAYERCLICK , onPlayerClick );
		}
		
		public function release():void
		{
			mainPlayer = null;
			netSocket = null;
			
			//LogicEventManager.instance.removeEventListener( LogicEvent.MOVE , onMove );
			LogicEventManager.instance.removeEventListener( LogicEvent.PLAYERCLICK , onPlayerClick );
		}
		
		public function get Pos():MapPos
		{
			return pos;
		}
		
		public function move( grid:MapGrid ):void
		{
			pos.mapGrid.copy( grid );
		}
		
		public function loadImg( posX:int , posY:int ):void
		{
			
		}
		
		public function onKeyUp( e:ModuleEvent ):void
		{
			if ( e.object.keyCode >= 49 && e.object.keyCode <= 60 ) 
			{
				mainPlayer.setAction( e.object.keyCode - 49 );
				mainPlayer.sendActionMsg( e.object.keyCode - 49 );
			}
			
		}
		
		public function onMouseClick( e:MouseEvent ):void
		{
			if ( !MapManager.instance.isLoaded )
			{
				return;
			}
			
			if ( SceneManager.instance.isFight() )
			{
				return;
			}
			
			if ( mainPlayer.team && mainPlayer.team.leader != mainPlayer )
			{
				return;
			}
			
			nowTime = getTimer();
			delay += nowTime - lastTime;
			lastTime = nowTime;
			
			if ( delay < 250 ) 
			{
				return;
			}
			
			delay = 0.0;
			
			if ( playerClick ) 
			{
				playerClick = false;
				return;
			}
			
			if ( mainPlayer.CheckState( GameCeatureState.FIGHT ) ) 
			{
				return;
			}
			
			var posX:Number = e.stageX + pos.mapGrid.realPosX;
			var posY:Number = e.stageY + pos.mapGrid.realPosY;
			
			var xx:int = posX / MapGrid.gridX - posY / MapGrid.gridY + 1;
			var yy:int = posY / MapGrid.gridY + posX / MapGrid.gridX + 0.5 - SceneManager.instance.sceneHalfY;
			
			//trace( xx , yy  , MapManager.instance.isBlock( xx , yy ) );
			
			var npcId:int = GameCreatureManager.instance.isNpc( xx , yy );
			
			if ( npcId != GameDefine.INVALIDID ) 
			{
				NpcNetHandler.sendTalkNpc( npcId , GameDefine.INVALIDID , GameDefine.INVALIDID );
				return;
			}
			
			if ( MapManager.instance.isBlock( xx , yy ) ) 
			{
				//PathFinder.instance.findPath( mainPlayer.MMapGrid.x , mainPlayer.MMapGrid.y , xx , yy );
				return;
			}
			
			
			if ( xx >= MapManager.instance.activeMapData.x || yy >= MapManager.instance.activeMapData.y ) 
			{
				
				return;
			}
			
			if ( mainPlayer.MMapGrid.x == xx && mainPlayer.MMapGrid.y == yy ) 
			{
				return;
			}
			
			
			//if ( !mainPlayer.pos.isCenter() ) 
			//{
				//return;
			//}
			
			//trace( mainPlayer.pos );
				
			//if ( MapManager.instance.isBlock( xx , yy ) ) 
			//{
				//PathFinder.instance.findPath( mainPlayer.MMapGrid.x , mainPlayer.MMapGrid.y , xx , yy );
			//	return;
			//}
			
			//trace( mainPlayer.mapPos.mapGrid , xx , yy );
			//trace( mainPlayer.movePath );
			
			
			
			
			mainPlayer.startMove( xx , yy );
		}
		
//		private function onMove( e:LogicEvent ):void
//		{
//			if ( e.object == mainPlayer ) 
//			{
//				
//			}
//			else
//			{
//				
//			}
//		}
		
		private function onPlayerClick( e:LogicEvent ):void
		{
			if ( e.object == mainPlayer ) 
			{
				return;
			}
			else
			{
				//if ( mainPlayer.isAttack || mainPlayer.isMoving ) 
				//{
				//	return;
				//}
				
				//mainPlayer.moveDirection = GameCreature.GetDirect( e.object.mapPos.mapGrid , mainPlayer.mapPos.mapGrid );
				//mainPlayer.changeActionMC();
				
				//mainPlayer.sendDirection();
				
				//playerClick = true;
			}
		}
		
		
		
	}
	
}


